﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBControl;

namespace SellerProject
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CheckTopSign(Request.QueryString["top_parameters"] + Request.QueryString["top_session"], Request.QueryString["top_sign"]))
                {
                    Session["sessionKey"] = Request.QueryString["top_session"];//取得用户session
                    string strParameters = Request.QueryString["top_parameters"];
                    SystemLib sb = new SystemLib();
                    Session.Add("usNick", sb.GetParameters(strParameters, "visitor_nick"));//取得用户昵称
                    Marketing mk = new Marketing(Session["sessionKey"].ToString());
                    try
                    {
                        Group mygrp = mk.addTaobaoGroup(Session["usNick"].ToString());
                        SqlDbHelper sdh = new SqlDbHelper();
                        System.Data.SqlClient.SqlParameter[] sps = {new System.Data.SqlClient.SqlParameter("@_usnick", Session["usNick"].ToString()),new System.Data.SqlClient.SqlParameter("@_sessionkey",Session["sessionKey"])};
                        sdh.ExecuteNonQuery("modUsSession", System.Data.CommandType.StoredProcedure, sps);
                    }
                    catch (Exception err)
                    {
                        string js = "<script language=javascript>alert('" + err.Message.ToString() + "');top.window.location.replace('sell404.aspx?errmsg='" + err.Message.ToString() + ")</script>";
                        HttpContext.Current.Response.Write(js);
                        return;
                    }
                    Response.Redirect("sellMain.aspx");
                    //Response.Redirect("ControlTaobao.aspx");
                }
                else
                {
                    string path = Page.ResolveUrl("http://container.api.taobao.com/container?appkey=" + System.Configuration.ConfigurationManager.AppSettings["AppKey"].ToString());
                    string js = "<script language=javascript>alert('签名验证失败，请重试！');top.window.location.replace('" + path + "')</script>";
                    HttpContext.Current.Response.Write(js);
                }
            }

        }
        /// <summary>
        /// 验证TOP签名
        /// </summary>
        /// <param name="parameters">topParamters</param>
        /// <param name="topsign">topSign</param>
        /// <returns>boolen</returns>
        public static bool CheckTopSign(string parameters, string topsign)
        {
            System.Text.StringBuilder strResult = new System.Text.StringBuilder();
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            strResult.Append(System.Configuration.ConfigurationManager.AppSettings["AppKey"].ToString());
            strResult.Append(parameters);
            strResult.Append(System.Configuration.ConfigurationManager.AppSettings["AppSecret"].ToString());
            byte[] bytes = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strResult.ToString()));
            return System.Convert.ToBase64String(bytes) == topsign;
        }

        protected int DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            DateTime1.CompareTo(DateTime2);
            TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            return Convert.ToInt16(ts.Minutes);
        }
    }
}